// pages/login/login.js
Page({
  data: {
    openid: '',
    nickname: '',
    avatar: ''
  },

  // 绑定输入事件
  onInputNickname: function (e) {
    this.setData({
      nickname: e.detail.value
    });
  },

  onInputAvatar: function (e) {
    this.setData({
      avatar: e.detail.value
    });
  },

  // 登录或注册函数
  loginOrRegister: function () {
    const { nickname, avatar } = this.data;

    // 简单的输入验证
    if (!this.validateInput(nickname, avatar)) {
      return;
    }

    wx.login({
      success: (res) => {
        if (res.code) {
          console.log('获取到的 code:', res.code);
          this.callLoginOrRegisterCloudFunction(nickname, avatar);
        } else {
          this.showErrorToast('登录失败！' + res.errMsg);
        }
      },
      fail: (err) => {
        this.showErrorToast('调用 wx.login 失败，请检查网络或稍后重试');
      }
    });
  },

  // 验证输入
  validateInput: function (nickname, avatar) {
    if (!nickname || !avatar) {
      wx.showToast({
        title: '昵称和头像地址不能为空',
        icon: 'none'
      });
      return false;
    }
    return true;
  },

  // 调用登录或注册云函数
  callLoginOrRegisterCloudFunction: function (nickname, avatar) {
    wx.cloud.callFunction({
      name: 'loginOrRegister',
      data: {
        // code: res.code,
        nickname: nickname,
        avatar: avatar
      },
      success: (cloudRes) => {
        const result = cloudRes.result;
        if (result.success) {
          this.handleLoginSuccess(result);
        } else {
          this.showErrorToast('登录或注册失败，请稍后重试');
        }
      },
      fail: (cloudErr) => {
        console.error('调用云函数失败:', cloudErr);
        this.showErrorToast('调用云函数失败，请检查网络或稍后重试');
      }
    });
  },

  // 处理登录成功
  handleLoginSuccess: function (result) {
    console.log("让我来看看结果是啥",result);
    const userId = result._id;
    const userNickname = result.nickname;
    const userAvatar = result.avatar;
    const deepseekId = result.deepseekId; // 获取 deepseekId
    const role=result.role?result.role:"0";
    const app = getApp();
    app.globalData.userId = userId;
    app.globalData.userNickname = userNickname;
    app.globalData.userAvatar = userAvatar;
    app.globalData.deepseekId = deepseekId; // 存储 deepseekId 到全局数据
    app.globalData.role=role;
    console.log("app.globalData",app.globalData.role,result.role);
    console.log("deepSeekId是全局变量",app.globalData.deepseekId);

    console.log('用户 _id:', userId);
    console.log('用户昵称:', userNickname);
    console.log('用户头像:', userAvatar);
    console.log('用户 deepseekId:', deepseekId);

    wx.showToast({
      title: '登录成功',
      icon: 'success',
      success: () => {
        // 检查是否是 TabBar 页面，如果是则使用 wx.switchTab
        wx.switchTab({
          url: '/pages/index/index',
          fail: (err) => {
            console.error('页面跳转失败:', err);
          }
        });
      }
    });
  },

  // 显示错误提示
  showErrorToast: function (message) {
    wx.showToast({
      title: message,
      icon: 'none'
    });
  }
});